package com.xbongbong.paas.controller;


import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.DistributorMessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.message.service.DistributorMessageService;
import com.xbongbong.pro.message.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/message/mq/marketingManagement")
public class MessageRabbitMqMarketingManagementController {

    private static final Logger LOG = LoggerFactory.getLogger(MessageRabbitMqMarketingManagementController.class);


    @Resource
    private MessageService messageService;
    @Resource
    private DistributorMessageService distributorMessageService;

    /**
     * Description: 上架推送
     *
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @since v1.0
     */
    @RequestMapping(value = "/marketing/online", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid DistributorMessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception {
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if (Objects.equals(messageRabbitMqDTO.getDistributorMark(), DistributorMarkEnum.DISTRIBUTOR.getCode())) {
                    distributorMessageService.pushDistributorMessage(messageRabbitMqDTO, PushTypeEnum.DISTRIBUTOR_MARKETING_ACTIVITY_MANAGEMENT_ONLINE_PUSH);
                }else {
                    messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.MARKETING_ACTIVITY_MANAGEMENT_ONLINE_PUSH);
                }
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 上架推送
     *
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @since v1.0
     */
    @RequestMapping(value = "/promotion/online", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String promotionOnline(@RequestBody @Valid DistributorMessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception {
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if (Objects.equals(messageRabbitMqDTO.getDistributorMark(), DistributorMarkEnum.DISTRIBUTOR.getCode())) {
                    distributorMessageService.pushDistributorMessage(messageRabbitMqDTO, PushTypeEnum.DISTRIBUTOR_PROMOTION_ACTIVITY_MANAGEMENT_ONLINE_PUSH);
                }else {
                    messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.PROMOTION_ACTIVITY_MANAGEMENT_ONLINE_PUSH);
                }
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 上架推送
     *
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @since v1.0
     */
    @RequestMapping(value = "/couponGet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String couponGet(@RequestBody @Valid DistributorMessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception {
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                distributorMessageService.pushDistributorMessage(messageRabbitMqDTO, PushTypeEnum.DISTRIBUTOR_COUPON_GET_PUSH);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 上架推送
     *
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @since v1.0
     */
    @RequestMapping(value = "/couponUse", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String couponUse(@RequestBody @Valid DistributorMessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception {
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                distributorMessageService.pushDistributorMessage(messageRabbitMqDTO, PushTypeEnum.DISTRIBUTOR_COUPON_USE_PUSH);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


}
